home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / LANG / C / LIB / UNIXLIB37B / !UnixLib37 / src / c / ctime < prev    next >
Text File  |  1996-11-09  |  4KB  |  218 lines

  1. /****************************************************************************
  2.  *
  3.  * $Source: /unixb/home/unixlib/source/unixlib37/src/c/RCS/ctime,v $
  4.  * $Date: 1996/10/30 21:58:59 $
  5.  * $Revision: 1.2 $
  6.  * $State: Rel $
  7.  * $Author: unixlib $
  8.  *
  9.  * $Log: ctime,v $
  10.  * Revision 1.2  1996/10/30 21:58:59  unixlib
  11.  * Massive changes made by Nick Burret and Peter Burwood.
  12.  *
  13.  * Revision 1.1  1996/04/19 21:26:42  simon
  14.  * Initial revision
  15.  *
  16.  ***************************************************************************/
  17.  
  18. static const char rcs_id[] = "$Id: ctime,v 1.2 1996/10/30 21:58:59 unixlib Rel $";
  19.  
  20. #include <time.h>
  21. #include <stdio.h>
  22. #include <ctype.h>
  23.  
  24. static char *__tdays[] =
  25. {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
  26. static char *__tdayl[] =
  27. {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
  28. static char *__tmonths[] =
  29. {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
  30. static char *__tmonthl[] =
  31. {"January", "February", "March", "April", "May", "June", "July", "August",
  32.  "September", "October", "November", "December"};
  33.  
  34. /* standard representations (take care to avoid making
  35.  * strftime() call itself recursively ad infinitum) */
  36.  
  37. static char *__dtrep = "%a %b %d %H:%M:%S %Y";
  38. static char *__drep = "%a %b %d %Y";
  39. static char *__trep = "%H:%M:%S";
  40.  
  41. char *
  42. asctime (register const struct tm *t)
  43.  
  44. {
  45.   static char _buf[26];
  46.   register char *buf = _buf;
  47.  
  48.   buf += strftime (buf, 24, __dtrep, t);
  49.   *buf++ = '\n';
  50.   *buf++ = 0;
  51.  
  52.   return (_buf);
  53. }
  54.  
  55. char *
  56. ctime (register const time_t * tp)
  57.  
  58. {
  59.   return (asctime (localtime (tp)));
  60. }
  61.  
  62. size_t
  63. strftime (register char *buf, register size_t max,
  64.       register const char *fmt, register const struct tm * t)
  65.  
  66. {
  67.   register int i = max;
  68.  
  69.   while (*fmt && i)
  70.     {
  71.       if (*fmt != '%' || *++fmt == '%')        /* left to right evaluation */
  72.     {
  73.       *buf++ = *fmt++, i--;
  74.       continue;
  75.     }
  76.  
  77.       {
  78.     register char *s;
  79.     register int j;
  80.  
  81.     switch (*fmt)
  82.       {
  83.       case 'a':
  84.         s = __tdays[t->tm_wday];
  85.       scp:while (i && (*buf = *s))
  86.           buf++, s++, i--;
  87.         break;
  88.       case 'A':
  89.         s = __tdayl[t->tm_wday];
  90.         goto scp;
  91.       case 'b':
  92.         s = __tmonths[t->tm_mon];
  93.         goto scp;
  94.       case 'B':
  95.         s = __tmonthl[t->tm_mon];
  96.         goto scp;
  97.       case 'c':
  98.         j = strftime (buf, i, __dtrep, t);
  99.         buf += j, i -= j;
  100.         break;
  101.       case 'd':
  102.         if (i >= 2)
  103.           {
  104.         sprintf (buf, "%2d", t->tm_mday);
  105.         buf += 2, i -= 2;
  106.           }
  107.         break;
  108.       case 'H':
  109.         if (i >= 2)
  110.           {
  111.         sprintf (buf, "%.2d", t->tm_hour);
  112.         buf += 2, i -= 2;
  113.           }
  114.         break;
  115.       case 'I':
  116.         j = t->tm_hour;
  117.         if (j > 12)
  118.           j -= 12;
  119.         if (i >= 2)
  120.           {
  121.         sprintf (buf, "%2d", j);
  122.         buf += 2, i -= 2;
  123.           }
  124.         break;
  125.       case 'j':
  126.         if (i >= 3)
  127.           {
  128.         sprintf (buf, "%3d", t->tm_yday);
  129.         buf += 3, i -= 3;
  130.           }
  131.         break;
  132.       case 'm':
  133.         if (i >= 2)
  134.           {
  135.         sprintf (buf, "%2d", t->tm_mon);
  136.         buf += 2, i -= 2;
  137.           }
  138.         break;
  139.       case 'M':
  140.         if (i >= 2)
  141.           {
  142.         sprintf (buf, "%.2d", t->tm_min);
  143.         buf += 2, i -= 2;
  144.           }
  145.         break;
  146.       case 'p':
  147.         s = (t->tm_hour > 12) ? "PM" : "AM";
  148.         goto scp;
  149.       case 'S':
  150.         if (i >= 2)
  151.           {
  152.         sprintf (buf, "%.2d", t->tm_sec);
  153.         buf += 2, i -= 2;
  154.           }
  155.         break;
  156.       case 'U':
  157.         j = t->tm_yday;
  158.         if (j > 2)
  159.           j += (4 - t->tm_wday);
  160.         if (i >= 2)
  161.           {
  162.         sprintf (buf, "%2d", j / 7);
  163.         buf += 2, i -= 2;
  164.           }
  165.         break;
  166.       case 'w':
  167.         *buf++ = t->tm_wday + '0';
  168.         i--;
  169.         break;
  170.       case 'W':
  171.         j = t->tm_yday;
  172.         if (j > 2)
  173.           j += (5 - ((j == t->tm_wday) ? j : 7));
  174.         if (i >= 2)
  175.           {
  176.         sprintf (buf, "%2d", j / 7);
  177.         buf += 2, i -= 2;
  178.           }
  179.         break;
  180.       case 'x':
  181.         j = strftime (buf, i, __drep, t);
  182.         buf += j, i -= j;
  183.         break;
  184.       case 'X':
  185.         j = strftime (buf, i, __trep, t);
  186.         buf += j, i -= j;
  187.         break;
  188.       case 'y':
  189.         if (i >= 2)
  190.           {
  191.         sprintf (buf, "%2d", t->tm_year);
  192.         buf += 2, i -= 2;
  193.           }
  194.         break;
  195.       case 'Y':
  196.         if (i >= 4)
  197.           {
  198.         sprintf (buf, "%4d", t->tm_year + 1900);
  199.         buf += 4, i -= 4;
  200.           }
  201.         break;
  202.       case 'Z':
  203.         s = (char *) t->tm_zone;
  204.         goto scp;
  205.         break;
  206.       default:
  207.         *buf++ = *fmt, i--;
  208.         break;
  209.       }
  210.     fmt++;
  211.       }
  212.     }
  213.  
  214.   *buf = 0;
  215.  
  216.   return ((*fmt) ? 0 : max - i);
  217. }
  218.